package com.heima.wemedia.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Autowired
    private WmNewsMapper newsMapper;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult findList(WmNewsPageReqDto wmNewsPageReqDto) {

        // 1. 检查参数
        wmNewsPageReqDto.checkParam();

        // 2. 分页条件查询
        IPage page = new Page(wmNewsPageReqDto.getPage(), wmNewsPageReqDto.getSize());
        LambdaQueryWrapper<WmNews> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        /**
         * 1. 状态精确查询，频道精确查询，时间范围查询，关键字的模糊查询
         * 2. 查询当前登录人的文章
         * 3. 按照发布时间倒序查询
         */
        if (wmNewsPageReqDto.getStatus() != null) {
            lambdaQueryWrapper.eq(WmNews::getStatus, wmNewsPageReqDto.getStatus());
        }
        if (wmNewsPageReqDto.getChannelId() != null) {
            lambdaQueryWrapper.eq(WmNews::getChannelId, wmNewsPageReqDto.getChannelId());
        }

        if (wmNewsPageReqDto.getBeginPubDate() != null && wmNewsPageReqDto.getEndPubDate() != null) {
            lambdaQueryWrapper.between(WmNews::getPublishTime, wmNewsPageReqDto.getBeginPubDate(), wmNewsPageReqDto.getEndPubDate());
        }

        if (StringUtils.isNoneBlank(wmNewsPageReqDto.getKeyword())) {
            lambdaQueryWrapper.like(WmNews::getTitle, wmNewsPageReqDto.getKeyword());
        }

        // 查当前登录人的文章
        lambdaQueryWrapper.eq(WmNews::getUserId, WmThreadLocalUtil.getUser().getId());

        // 按照发布时间倒序查找
        lambdaQueryWrapper.orderByDesc(WmNews::getPublishTime);

        page = page(page, lambdaQueryWrapper);

        // 3. 结果返回
        ResponseResult result =
                new PageResponseResult(wmNewsPageReqDto.getPage(), wmNewsPageReqDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }


    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;


    @Override
    public ResponseResult submitNews(WmNewsDto wmNewsDto) {

        // 0. 条件判断
        if (wmNewsDto == null || wmNewsDto.getContent() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 1. 保存或者修改文章
        WmNews wmNews = new WmNews();
        // 属性拷贝 属性名称和类相同才可以拷贝
        BeanUtils.copyProperties(wmNewsDto, wmNews);
        // 封账图片 list -> string
        if (wmNewsDto.getImages() != null && wmNewsDto.getImages().size() > 0) {
            // [1dasswq.jpg,weijqnr.jpg] --> 1dasswq.jpg,weijqnr.jpg
            String imageStr = StringUtils.join(wmNewsDto.getImages(), ",");
            wmNews.setImages(imageStr);
        }

        // 如果当前封面类型为自动 -1
        if (wmNewsDto.getType().equals(WemediaConstants.WM_NEWS_TYPE_AUTO)) {
            wmNews.setType(null);
        }

        saveOrUpdateWmNews(wmNews);

        // 2. 判断是否为草稿 如果是草稿，结束当前的方法
        if (wmNewsDto.getStatus().equals(WmNews.Status.NORMAL.getCode())) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        // 3. 不是草稿，保存文章内容图片与素材的关系
        // 获取到文章内容中的图片信息
        List<String> materials = ectractUrlInfo(wmNewsDto.getContent());

        saveRelativeInfoForContent(materials, wmNews.getId());

        // 4. 不是草稿，保存文章封面与素材的关系，如果当前布局是自动，需要匹配封面图片
        saveRelativeInfoForCover(wmNewsDto, wmNews, materials);


        // TODO 进行审核
        // TODO 开启审核文章
        wmNewsAutoScanService.autoScanWmNews(wmNews.getId());


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * TODO 第一个功能：如果当前封面类型为自动，则设置封面类型的 数据
     *      TODO 匹配规则1：1. 如果内容图片 >= 1 且 < 3 单图 type 设置为 1
     *      TODO 匹配规则2：2. 如果内容图片 >= 3 多图 type 设置为 3
     *      TODO 匹配规则3：3. 如果内容没有图片，无图 type 设置为 0
     * TODO 第二个功能：保存封面图片 与 素材的关系
     *
     * @param wmNewsDto
     * @param wmNews
     * @param materials
     */
    private void saveRelativeInfoForCover(WmNewsDto wmNewsDto, WmNews wmNews, List<String> materials) {

        List<String> images = wmNewsDto.getImages();


        // 1. 如果当前封面类型为自动，则设置封面类型的 数据
        if (wmNewsDto.getType().equals(WemediaConstants.WM_NEWS_TYPE_AUTO)) {
            // 多图
            if (materials.size() >= 3) {
                wmNews.setType(WemediaConstants.WM_NEWS_MANY_IMAGE);
                images = materials.stream().limit(3).collect(Collectors.toList());
            } else if (materials.size() >= 1 && materials.size() < 3) {
                // 单图
                wmNews.setType(WemediaConstants.WM_NEWS_SINGLE_IMAGE);
                images = materials.stream().limit(1).collect(Collectors.toList());
            } else {
                // 无图
                wmNews.setType(WemediaConstants.WM_NEWS_NONE_IMAGE);
            }


            // 修改文章
            if (images != null && images.size() > 0) {
                saveRelativeInfo(images, wmNews.getId(), WemediaConstants.WM_COVER_REFERENCE);
            }
            updateById(wmNews);

        }

        // 第二个功能：保存封面图片 与 素材的关系
        if (images != null && images.size() > 0) {
            saveRelativeInfo(images, wmNews.getId(), WemediaConstants.WM_COVER_REFERENCE);
        }

    }


    /**
     * 处理文章内容和素材 的 关系
     *
     * @param materials 素材相关的 id
     * @param newsId    文章的 相关 id
     */
    private void saveRelativeInfoForContent(List<String> materials, Integer newsId) {


        // 保存 封面图片与素材的 关系
        saveRelativeInfo(materials, newsId, WemediaConstants.WM_CONTENT_REFERENCE);

    }


    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    /**
     * 保存文章所有图片和素材的关系到数据库中
     *
     * @param materials 图片访问的 路径 集合
     * @param newsId    文章的 id
     * @param type
     */
    private void saveRelativeInfo(List<String> materials, Integer newsId, Short type) {

        if (materials != null && !materials.isEmpty()) {
            // 通过图片的 url 查询 素材的 id
            List<WmMaterial> dbMaterials =
                    wmMaterialMapper.selectList(Wrappers.<WmMaterial>lambdaQuery().in(WmMaterial::getUrl, materials));

            // 判断素材是否有效
            if (dbMaterials == null || dbMaterials.size() == 0) {
                // 手动抛出异常 1.能提示调用者素材失效了，2.能进行数据的回滚
                throw new CustomException(AppHttpCodeEnum.MATERIASL_REFERENCE_FAIL);
            }

            // 传来的 数量 不等于 数据库中 查询的  数据
            if (materials.size() != dbMaterials.size()) {
                throw new CustomException(AppHttpCodeEnum.MATERIASL_REFERENCE_FAIL);
            }


            List<Integer> idList = dbMaterials.stream().map(WmMaterial::getId).collect(Collectors.toList());

            // 批量保存
            wmNewsMaterialMapper.saveRelations(idList, newsId, type);

        }


    }

    /**
     * 提取文章内容中 的 图片信息
     *
     * @param content
     * @return
     */
    private List<String> ectractUrlInfo(String content) {

        List<String> materials = new ArrayList<>();

        List<Map> maps = JSON.parseArray(content, Map.class);
        for (Map map : maps) {
            if (map.get("type").equals("image")) {
                String imgUrl = (String) map.get("value");
                materials.add(imgUrl);
            }
        }

        return materials;

    }


    /**
     * TODO 保存或修改文章
     *
     * @param wmNews
     */
    private void saveOrUpdateWmNews(WmNews wmNews) {

        // 补全属性
        wmNews.setUserId(WmThreadLocalUtil.getUser().getId());
        wmNews.setCreatedTime(new Date());
        wmNews.setSubmitedTime(new Date());
        wmNews.setEnable((short) 1);  // 默认上架

        if (wmNews.getId() == null) {
            // 保存
            save(wmNews);

        } else {
            // 修改
            // 删除文章图片与素材的关系
            wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId, wmNews.getId()));
            updateById(wmNews);
        }
    }


    @Override
    public ResponseResult selectNewsById(Integer id) {

        Integer userId = WmThreadLocalUtil.getUser().getId();

        LambdaQueryWrapper<WmNews> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmNews::getId, id);
        lambdaQueryWrapper.eq(WmNews::getUserId, userId);

        WmNews wmNews = getOne(lambdaQueryWrapper);

        return ResponseResult.okResult(wmNews);

    }


    @Override
    @Transactional
    public ResponseResult deleteNewsById(Integer id) {

        if (id == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章不存在");
        }

        WmNews wmNews = getById(id);

        if (wmNews != null) {
            if (wmNews.getStatus() == WemediaConstants.WM_NEWS_STATUS_CAOGAO) {
                this.removeById(id);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }

            if (wmNews.getStatus() == WemediaConstants.WM_NEWS_STATUS_DAISHENHE) {
                this.removeById(id);
                int row = wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery()
                        .eq(WmNewsMaterial::getNewsId, id));
                System.out.println("row = " + row);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }


            if (wmNews.getStatus() == WemediaConstants.WM_NEWS_STATUS_YIFABU) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章已发布，不能删除");
            }

        }


        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章不存在");
    }


    @Override
    public ResponseResult downOrUp(WmNewsDto dto) {

        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章ID不可少");
        }

        WmNews wmNews = getById(dto.getId());

        if (wmNews == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在");
        }


        if (wmNews.getStatus() != WemediaConstants.WM_NEWS_STATUS_YIFABU) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文章未发布，不可删除");
        }


        if (dto.getEnable() == 1) {
            dto.setEnable(WemediaConstants.WM_NEWS_NONE_IMAGE);
        }else if (dto.getEnable() == 0) {
            dto.setEnable(WemediaConstants.WM_NEWS_SINGLE_IMAGE);
        }

        updateById(wmNews);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
















